package com.lcx.hw.controller;

import com.lcx.hw.service.EmailService;
import com.lcx.hw.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(maxAge=3600)//解决跨域
@RequestMapping("/code")
public class CodeController {

    //远程调用user接口
    @Reference
    private UserService userService;

    //远程调用email接口
    @Reference
    private EmailService emailService;

    @GetMapping("/create/{email}")
    public Object create(@PathVariable(value = "email")String email){
        try {
            //判断这个邮箱是否已经注册过
            if (userService.isRegistered(email)){
                return false;
            }
            Integer v = (int) ((Math.random() * 9 + 1) * 100000);
            //这里的密码暂时先保存0，因为为空会造成路径404
            Integer register = userService.register(email, "0", v.toString());
            //发送邮件
            return emailService.send(email,v.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
